package com.ruoyi.kenuo.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.kenuo.domain.Equ;
import com.ruoyi.kenuo.domain.KernelUserEqu;
import com.ruoyi.kenuo.mapper.EquMapper;
import com.ruoyi.kenuo.mapper.KernelUserEquMapper;
import com.ruoyi.kenuo.service.IKernelUserEquService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 用户和设备的关系Service业务层处理
 *
 * @author polo
 * @date 2022-10-17
 */
@Service
public class KernelUserEquServiceImpl implements IKernelUserEquService
{
    @Autowired
    private KernelUserEquMapper kernelUserEquMapper;
    @Autowired
    private EquMapper equMapper;

    /**
     * 查询用户和设备的关系
     *
     * @param id 用户和设备的关系主键
     * @return 用户和设备的关系
     */
    @Override
    public KernelUserEqu selectKernelUserEquById(String id)
    {
        return kernelUserEquMapper.selectKernelUserEquById(id);
    }

    /**
     * 查询用户和设备的关系列表
     *
     * @param kernelUserEqu 用户和设备的关系
     * @return 用户和设备的关系
     */
    @Override
    public List<KernelUserEqu> selectKernelUserEquList(KernelUserEqu kernelUserEqu)
    {
        return kernelUserEquMapper.selectKernelUserEquList(kernelUserEqu);
    }

    /**
     * 新增用户和设备的关系
     *
     * @param kernelUserEqu 用户和设备的关系
     * @return 结果
     */
    @Override
    public int insertKernelUserEqu(KernelUserEqu kernelUserEqu)
    {
        String id = kernelUserEqu.getId();
        if(StringUtils.isEmpty(id)){
            kernelUserEqu.setId(IdUtils.fas32tUUID());
        }
        kernelUserEqu.setCreateTime(DateUtils.getNowDate());
        //查询设备是否被绑定
        List<KernelUserEqu> elist = kernelUserEquMapper.findEuqUseBySn(kernelUserEqu.getSnCode());
        if(elist.size()>0){
            return 11;
        }
        //去绑定设备
        int t = kernelUserEquMapper.insertKernelUserEqu(kernelUserEqu);
        if(t>0){
            //去修改设备的绑定状态
            Equ e = new Equ();
            e.setEquipmentSn(kernelUserEqu.getSnCode());
            e.setBoundStatus(2);
            e.setUpdateTime(DateUtils.getNowDate());
            e.setBoundTime(DateUtils.getNowDate());
            e.setBoundPhone(kernelUserEqu.getRemarkOne());
            e.setEquipmentName(kernelUserEqu.getEquipmentName()==null?"":kernelUserEqu.getEquipmentName());
            e.setElectic(kernelUserEqu.getElectic()==null?"":kernelUserEqu.getElectic());
            e.setServial(kernelUserEqu.getServial()==null?"":kernelUserEqu.getServial());
            e.setMac(kernelUserEqu.getMac()==null?"":kernelUserEqu.getMac());
            e.setVersion(kernelUserEqu.getVersion()==null?"":kernelUserEqu.getVersion());
            e.setNearTime(DateUtils.getNowDate());
            equMapper.updateEquBySn(e);
        }else {
            t=0;
        }
        return t;
    }

    /**
     * 修改用户和设备的关系
     *
     * @param kernelUserEqu 用户和设备的关系
     * @return 结果
     */
    @Override
    public int updateKernelUserEqu(KernelUserEqu kernelUserEqu)
    {
        kernelUserEqu.setUpdateTime(DateUtils.getNowDate());
        return kernelUserEquMapper.updateKernelUserEqu(kernelUserEqu);
    }

    /**
     * 批量删除用户和设备的关系
     *
     * @param ids 需要删除的用户和设备的关系主键
     * @return 结果
     */
    @Override
    public int deleteKernelUserEquByIds(String[] ids)
    {
        return kernelUserEquMapper.deleteKernelUserEquByIds(ids);
    }

    /**
     * 删除用户和设备的关系信息
     *
     * @param id 用户和设备的关系主键
     * @return 结果
     */
    @Override
    public int deleteKernelUserEquById(String id)
    {
        return kernelUserEquMapper.deleteKernelUserEquById(id);
    }

    @Override
    public AjaxResult getUserEqu(KernelUserEqu kernelUserEqu) {
        List<KernelUserEqu>  ueList =  kernelUserEquMapper.selectKernelUserEquList(kernelUserEqu);
        if(ueList.size()==0){
           return AjaxResult.error(null);
        }
        return AjaxResult.success(ueList);
    }

    @Override
    public AjaxResult removeEquUser(String snCode, String tel) {
        kernelUserEquMapper.removeEquUser(snCode,tel);
        //去修改设备的绑定状态为未绑定
        //去修改设备的绑定状态
        Equ e = new Equ();
        e.setEquipmentSn(snCode);
        e.setBoundStatus(1);
        e.setUpdateTime(DateUtils.getNowDate());
        e.setUnboundTime(DateUtils.getNowDate());
        e.setBoundTime(null);
        e.setBoundPhone("");
        e.setNearTime(DateUtils.getNowDate());
        equMapper.updateEquBySn(e);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getUserEquByZh(KernelUserEqu kernelUserEqu) {
        List<Map<String,Object>>  ueList =  kernelUserEquMapper.getUserEquByZh(kernelUserEqu);
        if(ueList.size()>0){
            return AjaxResult.success("操作成功",ueList,true);
        }else {
            return AjaxResult.success("操作成功",null,false);
        }
    }
}
